<?php

abstract class HTech_Db_Table extends Zend_Db_Table_Abstract
{   
    protected $_record;
    protected $_lnkTables = array();    
    protected $_gridMetadata = array('title' => '', 'titleMods' => array(), 
        'filter' => true, 'params' => array(), 'headers' => array(), 'onClick' => '');
           
    public function save($data) 
    {
        $primary = current($this->info('primary'));
        $id = $data[$primary];
        $lnk = array();
        foreach($this->_lnkTables as $lnkTable => $key) {
            if(is_array($data[$lnkTable])) {                
                foreach($data[$lnkTable] as $v) {
                    $d = array();
                    $d[$primary] = $id;
                    $d[$key] = $v;
                    $lnk[$lnkTable][] = $d; 
                }     
            }
            unset($data[$lnkTable]);
        }
        $this->getDefaultAdapter()->beginTransaction();       
        try {
            if ($id) {
                $this->_deleteLnkRows($id);
                $this->update($data,sprintf('%s = %s', $primary, $id));
            } else {
                $id = $this->insert($data);
            }
            $this->_insertLnkRows($lnk); 
            $this->_addCallback($id, $data);
        } catch (Zend_Exception $e) {
            $this->getDefaultAdapter()->rollBack();                   
            throw new Exception($e->getMessage());
            return;
        }  
        $this->getDefaultAdapter()->commit();
        return $id;
    }

    public function getGridMetadata()
    {
        if(count($this->_gridMetadata['headers']) > 0) return $this->_gridMetadata;
        return $this->_setGridMetadata()->_gridMetadata;
    }
    
    public function getRecord($id)
    {
        if ($this->_record) return $this->_record;
        $this->_record = (object) array('d' => null, 'r' => null, 'l' => null,'p' => null); 
        $rowset = parent::find($id);
        if(count($rowset) != 1) throw new Exception(Zend_Registry::get('Zend_Translate')->_('Invalid record request'));
        $this->_record->d = $rowset->current();
        $this->_setRecordPerformance();    
        $this->_setRecordDependentRows();
        return $this->_record;
    }
    
    public function resetRecord()
    {
        if ($this->_record) $this->_record = null; 
    }
    
    abstract protected function _setGridMetadata();
  
    protected function _setRecordPerformance() {}
    
    protected function _setRecordDependentRows() {}  
    
    protected function _setRecordLnkData($item, $key, $param) 
    {
        $this->_record->l[$param[0]][$key] = $item[$param[1]];
    }
    
    protected function _addCallback($id) { return $id; }
    
    private function _deleteLnkRows($id) 
    {
        foreach(array_keys($this->_lnkTables) as $table) {
            $lnkModel = new $table();
            $lnkModel->delete(sprintf('%s = %s', current($this->info('primary')), $id));
        }
    }
    
    private function _insertLnkRows($data) 
    { 
        foreach(array_keys($this->_lnkTables) as $table) {
            $lnkModel = new $table();
            foreach($data[$table] as $row) {
                $lnkModel->insert($row);                       
            }
        }    
    }
        
    public function translate($value)
    {            
        return Zend_Registry::get('Zend_Translate')->_($value);
    }
}